// Tencent is pleased to support the open source community by making
// 蓝鲸智云 - 监控平台/日志平台 (BlueKing - Monitor/Log) available.
// Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//

package monitoring

import "github.com/prometheus/client_golang/prometheus"

const (
	Namespace = "bk_collector"
)

func init() {
	prometheus.MustRegister(
		ControllerReloadSuccessTotal, // controller reload 成功计数器
		ControllerReloadErrorTotal,   // controller reload 失败计数器
		ReceiverHandledTotal,         // receiver 接收并发送到队列数据计数器
		ReceiverDroppedTotal,         // receiver 丢弃数据计数器（数据异常）
		ReceiverSkippedTotal,         // receiver 接收空数据总数（请求数据为空）
		ReceiverInternalErrorTotal,   // receiver 接收触发异常总数（服务端异常）
		ReceiverHandledDuration,      // receiver 接收平均耗时（ms）
		ReceiverReceivedTotal,        // receiver 请求数据量计数器（metrics/logs/traces 数量）
		ExporterSentDuration,         // exporter 发送平均耗时（ms）
		ExporterSentTotal,            // exporter 发送数据计数器
		PipelineBuiltErrorTotal,      // pipeline 构建失败计数器
		PipelineBuiltSuccessTotal,    // pipeline 构建成功计数器
		PipelineDroppedTotal,         // pipeline 丢弃数据计数器（processor 返回 error）
		PipelineSkippedTotal,         // pipeline 空数据计数器（processor 处理后为空数据）
		PipelineHandledTotal,         // pipeline 处理并发送到队列数据计数器
		PipelineHandledDuration,      // pipeline 处理平均耗时（ms）
	)
}

// source: oneapm/ot
// protocol: http/grpc
// record_type: metrics/logs/traces

var (
	ControllerReloadSuccessTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "controller_reload_success_total",
		},
		nil,
	)

	ControllerReloadErrorTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "controller_reload_error_total",
		},
		nil,
	)

	ReceiverHandledTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "receiver_handled_total",
		},
		[]string{"source", "protocol", "record_type"},
	)

	ReceiverDroppedTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "receiver_dropped_total",
		},
		[]string{"source", "protocol", "record_type"},
	)

	ReceiverSkippedTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "receiver_skipped_total",
		},
		[]string{"source", "protocol", "record_type"},
	)

	ReceiverInternalErrorTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "receiver_internal_error_total",
		},
		[]string{"source", "protocol", "record_type"},
	)

	ReceiverHandledDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "receiver_handled_duration_in_ms",
			Buckets:   []float64{1, 5, 10, 50, 100, 500, 1000, 5000},
		},
		[]string{"source", "protocol", "record_type"},
	)

	ReceiverReceivedTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "receiver_received_total",
		},
		[]string{"source", "protocol", "record_type"},
	)

	ExporterSentDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "exporter_sent_duration_in_ms",
			Buckets:   []float64{1, 5, 10, 50, 100, 500, 1000, 5000},
		},
		nil,
	)

	ExporterSentTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "exporter_sent_total",
		},
		[]string{"record_type", "id"},
	)

	PipelineBuiltErrorTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "pipeline_built_error_total",
		},
		[]string{"pipeline", "record_type"},
	)

	PipelineBuiltSuccessTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "pipeline_built_success_total",
		},
		[]string{"pipeline", "record_type"},
	)

	PipelineDroppedTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "pipeline_dropped_total",
		},
		[]string{"pipeline", "record_type", "id", "processor"},
	)

	PipelineSkippedTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "pipeline_skipped_total",
		},
		[]string{"pipeline", "record_type", "id", "processor"},
	)

	PipelineHandledTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "pipeline_handled_total",
		},
		[]string{"pipeline", "record_type", "id"},
	)

	PipelineHandledDuration = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "pipeline_handled_duration_in_ms",
			Buckets:   []float64{1, 10, 50, 100, 500, 1000, 10000},
		},
		[]string{"pipeline", "record_type", "id"},
	)
)
